package com.koocloud.electroplatemanage.auth.mapper;

import java.util.List;
import java.util.Map;

import com.koocloud.electroplatemanage.pojo.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;



@Mapper
public interface IPermissionDao {
	@Select("Select * from permission where "+ "(case when #{id} is not null and #{id}!= '' then (id like '%${id}%') else (1=1) end) and "+" (case when #{permissionName} is not null and #{permissionName}!= '' then (permission_name like '%${permissionName}%') else (1=1) end) ")
	List<Permission> selectPermission(@Param("id")String id, @Param("permissionName")String permissionName);
	
	@Update("update permission set id=#{id},permission_code=#{permissionCode},permission_name=#{permissionName},url=#{url},icon=#{icon},`order`=#{order},parent_id=#{parentId},lvl=#{lvl},type=#{type}  where id=#{id}")
	Integer updatePermission(Permission permission);

	@Delete("delete from permission where id=#{id}")
	Integer deletePermission(String id);

	@Select("select id from permission where permission_name=#{authName}")
	String selectRemarksId(String authName);
	
	@Delete("delete from role_permission where role_id=#{id}")
	Integer deleteRoleAuthRelation(String id);
	
	
	@Insert("insert into role_permission(id,role_id,permission_id) values(#{ids},#{id},#{authid})")
	Integer insert(@Param("ids")String ids, @Param("id")String id,@Param("authid") String authid);

	@Select("select * from permission")
	List<Map<String, Object>> selectPermissionList();

	@Insert("insert into permission(permission_code,permission_name,url,icon,`order`,parent_id,lvl,type) values(#{permissionCode},#{permissionName},#{url},#{icon},#{order},#{parentId},#{lvl},#{type})")
	@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//加入该注解可以保持对象后，查看对象插入id
	Integer addPermission(Permission permission);

	@Select("Select count(*) from permission where "+ "(case when #{id} is not null and #{id}!= '' then (id like '%${id}%') else (1=1) end) and "+" (case when #{permissionName} is not null and #{permissionName}!= '' then (permission_name like '%${permissionName}%') else (1=1) end)")
	Integer selectPermission2(@Param("id")String id,@Param("permissionName") String permissionName);

	
	@Select("select id,permission_name from permission where  parent_id=-1")
	List<Map<String, Object>> selectFirstPermissionList();
	
	@Select("select * from permission where id=#{id}")
	Permission  selectPermissionById(String id);

	@Select("SELECT count(*) FROM permission  where parent_id=#{id}")
	String selectChirenNumById(String id);

	@Select("select * from permission where permission_name=#{permissionName}")
    Integer selectPermissionByName(String permissionName);

	@Insert("insert into permission(permission_code,permission_name,url,icon,`order`,parent_id,lvl,type) values(#{permissionCode},#{permissionName},#{url},#{icon},#{order},#{parentId},#{lvl},#{type})")
	@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//加入该注解可以保持对象后，查看对象插入id
    Integer insertPermission2(Permission permission2);

	@Select("select * from permission where parent_id=#{id}")
    List<Permission> selectPermissionByParentId(String id);

	@Select("  SELECT  GROUP_CONCAT(p.permission_name) has_auth_list FROM role r inner JOIN role_permission rp\r\n" + 
	        "    on r.id=rp.role_id\r\n" + 
	        "    inner JOIN permission p on rp.permission_id=p.id\r\n" + 
	        "    where r.id=#{id}")
    String selectAllPermissionStrByRoleId(long id);

	


}
